<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/backHome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="margin-top:20px;">
      <el-row>
        <el-button type="primary">添加角色</el-button>
      </el-row>

      <el-table :data="rolesList" border stripe style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bd-bottom',index1==0?'bd-top':'','vCenter']"
              v-for="(item1,index1) in scope.row.children"
              :key="item1.id"
            >
              <el-col :span="6">
                <el-tag @close="removeRight(scope.row,item1.id)" closable>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="18">
                <el-row
                  :class="[index2==0?'':'bd-top','vCenter']"
                  v-for="(item2,index2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="7">
                    <el-tag
                      @close="removeRight(scope.row,item2.id)"
                      closable
                      type="success"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="17">
                    <el-tag
                      @close="removeRight(scope.row,item3.id)"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      type="warning"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="editDialog(scope.row)"
              size="mini"
              icon="el-icon-edit"
            >编辑</el-button>
            <el-button
              @click="delUser(scope.row)"
              type="danger"
              size="mini"
              icon="el-icon-delete"
            >删除</el-button>
            <el-button
              @click="rolesDialog(scope.row)"
              type="warning"
              size="mini"
              icon="el-icon-s-tools"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分配权限对话框 -->
      <el-dialog title="分配权限" :visible.sync="dialogRolesVisible">
        <el-tree ref='treeRef' :default-checked-keys='defalutKeys' :default-expand-all='true' node-key='id' :data="rightsList" :props="defaultProps" show-checkbox></el-tree>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="setRoles">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: "demo", // 组件参数 接收来自父组件的数据
  props: [],
  components: {},
  data() {
    return {
      rolesList: [],
      dialogRolesVisible: false,
      rightsList: [],
      defaultProps: {
        children: "children",
        label: "authName"
      },
      defalutKeys:[],
      rolesInfo:'',
    };
  },
  computed: {},
  watch: {},
  methods: {
    async getRoles() {
      const { data: res } = await this.$axios.get("roles");
      if (res.meta.status !== 200)
        return this.$message.error("获取角色列表失败");
      this.rolesList = res.data;
      console.log(this.rolesList);
    },
    async removeRight(row, rightId) {
      // console.log(rightId)
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult == "cancel") return this.$message.info("取消操作");
      const { data: res } = await this.$axios.delete(
        `roles/${row.id}/rights/${rightId}`
      );
      if (res.meta.status !== 200) return this.$message.error("删除权限失败");
      row.children = res.data;
      this.$message.success("删除权限成功");
    },
    async rolesDialog(info) {
        this.rolesInfo = info
        this.defalutKeys =[]
      console.log(info);
      //   rights/:type
      const { data: res } = await this.$axios.get("rights/tree");
      if (res.meta.status !== 200)
        return this.$message.error("获取权限列表失败");
      this.rightsList = res.data;
      console.log(this.rightsList);
        this.getDefaultKeys(info,this.defalutKeys)
      this.dialogRolesVisible = true;
    },
    getDefaultKeys(node,arr){
        if( !node.children ){
            return arr.push(node.id)
        }
        node.children.forEach( item=>{
            this.getDefaultKeys(item,arr)
        } )
    },
    async setRoles(){
        var check = [
            ...this.$refs.treeRef.getCheckedKeys(),
            ...this.$refs.treeRef.getHalfCheckedKeys(),
        ]
        // roles/:roleId/rights
        var checkKeys = check.join(',')
        // console.log(this.rolesId)
        const {data:res} = await this.$axios.post(`roles/${this.rolesInfo.id}/rights`,{rids:checkKeys})
        if( res.meta.status !== 200 ) return this.$message.error('分配权限失败')
        // this.rolesInfo.children = res.data
        this.dialogRolesVisible = false;
        this.getRoles()
        this.$message.success('分配权限成功')
    }
  },
  beforeCreate() {},
  created() {},
  beforeMount() {},
  mounted() {
    this.getRoles();
  }
};
</script> 
<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.bd-top {
  border-top: 1px solid #ddd;
}
.bd-bottom {
  border-bottom: 1px solid #ddd;
}
.el-tag {
  margin: 10px;
}
.vCenter {
  display: flex;
  align-items: center;
}
</style>
